package com.github.eclipseace.mybatis.spring;

import java.io.InputStream;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

import com.github.eclipseace.mybatis.entity.builder.ElementBuilder;
import com.github.eclipseace.mybatis.entity.builder.xml.XmlElementBuilder;

/**
 * @author eclipseAce
 */
public class XmlElementBuilderFactoryBean implements FactoryBean<ElementBuilder> {
	private Resource location;

	@Override
	public ElementBuilder getObject() throws Exception {
		Assert.notNull(location, "Property 'location' is required");

		InputStream stream = location.getInputStream();
		try {
			return new XmlElementBuilder(stream);
		} finally {
			stream.close();
		}
	}

	@Override
	public Class<?> getObjectType() {
		return XmlElementBuilder.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	public Resource getLocation() {
		return location;
	}

	public void setLocation(Resource location) {
		this.location = location;
	}
}
